//===============================
//  功能 ：备注 
//  作者 ：上中野辅亚瑟王 
//  创建时间 ：2024-08-16  11-24-11 
//  Unity版本  ：2019.4.35f1 
//  变更时间 :  2024-08-16  11-24-11 
//===============================





using UnityEngine;

public static class RectTransformInfo
{
    public static void Get(RectTransform rectTransform,string key="")
    {
        if (PlayerPrefs.HasKey($"{rectTransform.name.GetHashCode()}{key}"))
        {
            rectTransform.anchorMin = GetVector2($"anchorMin: {rectTransform.name.GetHashCode()}{key}");
            rectTransform.anchorMax = GetVector2($"anchorMax: {rectTransform.name.GetHashCode()}{key}");
            rectTransform.pivot = GetVector2($"pivot: {rectTransform.name.GetHashCode()}{key}");
            rectTransform.sizeDelta = GetVector2($"sizeDelta: {rectTransform.name.GetHashCode()}{key}");

            rectTransform.position = GetVector3($"position: {rectTransform.name.GetHashCode()}{key}");
            rectTransform.localRotation = Quaternion.Euler(GetVector3($"localRotation: {rectTransform.name.GetHashCode()}{key}"));
            rectTransform.localScale = GetVector3($"localScale: {rectTransform.name.GetHashCode()}{key}");
        }
    }

    public static void Save(RectTransform rectTransform,string key="")
    {
        PlayerPrefs.SetString($"{rectTransform.name.GetHashCode()}{key}", "");
        SetVector2($"anchorMin: {rectTransform.name.GetHashCode()}{key}", rectTransform.anchorMin);
        SetVector2($"anchorMax: {rectTransform.name.GetHashCode()}{key}", rectTransform.anchorMax);
        SetVector2($"pivot: {rectTransform.name.GetHashCode()}{key}", rectTransform.pivot);
        SetVector2($"sizeDelta: {rectTransform.name.GetHashCode()}{key}", rectTransform.sizeDelta);

        SetVector3($"position: {rectTransform.name.GetHashCode()}{key}", rectTransform.position);
        SetVector3($"localRotation: {rectTransform.name.GetHashCode()}{key}", rectTransform.localRotation.eulerAngles);
        SetVector3($"localScale: {rectTransform.name.GetHashCode()}{key}", rectTransform.localScale);
    }

    private static void SetVector2(string key, Vector2 value)
    {
        PlayerPrefs.SetString(key, value.x + "|" + value.y);
    }

    private static Vector2 GetVector2(string key)
    {
        string[] values = PlayerPrefs.GetString(key).Split('|');
        return new Vector2(float.Parse(values[0]), float.Parse(values[1]));
    }


    private static void SetVector3(string key, Vector3 value)
    {
        PlayerPrefs.SetString(key, JsonUtility.ToJson(value));
    }

    private static Vector3 GetVector3(string key)
    {
        return JsonUtility.FromJson<Vector3>(PlayerPrefs.GetString(key));
    }
}
